package com.powernode.controller;

import com.powernode.config.UploadProperties;
import com.powernode.domain.SysAttachment;
import com.powernode.domain.SysFolder;
import com.powernode.service.SysAttachmentService;
import com.powernode.service.SysFolderService;
import com.powernode.service.UploadService;
import com.powernode.utils.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：文件管理
 * 作者：动力节点·武汉
 */
@Controller
@RequestMapping("attachment")
public class AttachmentController {

    @Resource
    private SysAttachmentService attachmentService;

    @Resource
    private SysFolderService folderService;

    @Resource
    private UploadService uploadService;


    /**
     * 根据当前用户查询文件并跳转到templates/attachment/list.html
     */
    @GetMapping("queryAttachmentList")
    public String queryAttachmentList(String keywords, Model model){
        //解决模糊查询之后丢失关键字的问题
        model.addAttribute("keywords",keywords);
        //得到当前用户对象
        Integer userId= WebUtils.getCurrentUser().getUserId();
        List<SysAttachment> attachmentList=attachmentService.queryAttachmentList(keywords,userId);
        model.addAttribute("attachmentList",attachmentList);
        return "attachment/list";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("toAdd")
    public String toAdd(Model model){
        Integer userId=WebUtils.getCurrentUser().getUserId();
        //根据用户ID查询文件夹
        List<SysFolder> folderList = this.folderService.queryFolderList(null, userId);
        model.addAttribute("folderList",folderList);
        return "attachment/add";
    }
    /**
     * 添加
     */
    @PostMapping("add")
    public String add(MultipartFile [] mf,String fileMark,Integer folderId){
        try {
            for (MultipartFile file : mf) {
                //构造SysAttachment
                SysAttachment sysAttachment=new SysAttachment();
                sysAttachment.setCreateTime(new Date());
                sysAttachment.setFileMark(fileMark);
                sysAttachment.setFolderId(folderId);
                sysAttachment.setOldName(file.getOriginalFilename());
                sysAttachment.setFileType(file.getContentType());
                sysAttachment.setFileSize(new Integer(file.getSize()+""));
                Map<String, Object> objectMap = uploadService.uploadImage(file);
                String fullPath = objectMap.get("fullPath").toString();
                sysAttachment.setFilePath(fullPath);
                //保存
                this.attachmentService.insertSelective(sysAttachment);
            }
        }catch (Exception e){
           e.printStackTrace();
        }
        return "redirect:/attachment/queryAttachmentList";
    }

    @Resource
    private UploadProperties properties;
    /**
     * 删除
     */
    @GetMapping("delete")
    public String delete(Integer id){
        //根据id查询文件对象
        SysAttachment sysAttachment = this.attachmentService.selectByPrimaryKey(id);
        String fullPath=sysAttachment.getFilePath();
        //删除文件服务器的数据
        this.uploadService.deleteFile(fullPath.replace(properties.getBaseUrl(),""));
        //删除数据库里面的数据
        this.attachmentService.deleteByPrimaryKey(id);
        return "redirect:/attachment/queryAttachmentList";
    }

}
